﻿using Library.OrmEnterpriseEntity.Factor;
using Library.OrmEnterpriseEntity.TableInfo;

namespace Library.OrmEnterpriseEntity {
	///<summary>
	///</summary>
	public class PersistentEntity : GlobalEntity {
		protected PersistentEntityView PersistentEntityMetadata { get; private set; }


		private readonly Int16Column _rowType;


		protected PersistentEntity(PersistentEntityView metadata)
			: base(metadata) {
			BeginInit();
			PersistentEntityMetadata = metadata;
			_rowType = new Int16Column(PersistentEntityMetadata.RowType, this);
			if (!metadata.CrossRow)
				_rowType.Value = DbContext.Instance.Context.CurrentRowType;

			EndInit();
		}
		internal override QueryExpBuilder BuildWhereInLoadMethod(params DbColumn[] columns) {
			QueryExpBuilder where = null;
			foreach (var dbColumn in columns) {
				if (dbColumn != RowType)
					where = AndOrCreate(where, dbColumn.ColumnView, QueryOperator.Equal, dbColumn.DbValue);
			}
			return where;
		}


		///<summary>
		///</summary>
		public Int16Column RowType { get { return _rowType; } }
		internal override Int16Column ColumnRowType {
			get { return _rowType; }
		}
	}
}
